<template>
  <el-form ref="fltForm" label-width="155px" :model="FLT_INFO" size="mini">
    <el-row>
      <el-col :span="24">
        <el-form-item
          :rules="rules.dptType"
          @click.native="getDptPre"
          label="集团/糖厂/货主: "
          label-width="155px"
          prop="dptType"
        >
          <el-radio v-model="FLT_INFO.dptType" label="JT">集团</el-radio>
          <el-radio v-model="FLT_INFO.dptType" label="TC">糖厂</el-radio>
          <el-radio v-model="FLT_INFO.dptType" label="HZ">货主</el-radio>
        </el-form-item>
      </el-col>
    </el-row>
    <!--    <el-row>-->
    <!--      <el-col :span="24">-->
    <!--        <el-form-item label="集团/糖厂/货主序号: " label-width="155px" prop="dptSeq" :rules="rules.dptSeq">-->
    <!--          <el-input :disabled="disabled" maxlength="127" style="width: 100%" v-model="FLT_INFO.dptSeq"></el-input>-->
    <!--        </el-form-item>-->
    <!--      </el-col>-->
    <!--    </el-row>-->

    <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="集团/糖厂/货主代码: " label-width="155px" prop="dptCode" :rules="rules.dptCode">
              <el-input :disabled="!isAdd" maxlength="10" style="width: 100%" v-model="FLT_INFO.dptCode"></el-input>
            </el-form-item>
          </el-col>
    </el-row>-->

    <el-row>
      <el-col :span="24">
        <el-form-item
          label="集团/糖厂/货主名称: "
          label-width="155px"
          prop="dptName"
          :rules="rules.dptName"
        >
          <el-input
            :disabled="disabled"
            maxlength="37"
            style="width: 100%"
            v-model="FLT_INFO.dptName"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item
          label="集团/糖厂/货主简称: "
          label-width="155px"
          prop="dptAbbreviation"
          :rules="rules.dptAbbreviation"
        >
          <el-input
            :disabled="disabled"
            maxlength="37"
            style="width: 100%"
            v-model="FLT_INFO.dptAbbreviation"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="地址: " label-width="155px" prop="dptAddress" :rules="rules.dptAddress">
          <el-input
            :disabled="disabled"
            maxlength="37"
            style="width: 100%"
            v-model="FLT_INFO.dptAddress"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="联系人: " label-width="155px" prop="dptContact" :rules="rules.dptContact">
          <el-input
            :disabled="disabled"
            maxlength="37"
            style="width: 100%"
            v-model="FLT_INFO.dptContact"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="联系人电话: " label-width="155px" prop="dptPhone" :rules="rules.dptPhone">
          <el-input
            :disabled="disabled"
            maxlength="37"
            style="width: 100%"
            v-model="FLT_INFO.dptPhone"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="联系人邮箱：" prop="dptEmail" label-width="155px" :rules="rules.dptEmail">
          <el-input maxlength="30" v-model="FLT_INFO.dptEmail"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item
          label="法人代表: "
          label-width="155px"
          prop="dptCorporation"
          :rules="rules.dptCorporation"
        >
          <el-input
            :disabled="disabled"
            maxlength="37"
            style="width: 100%"
            v-model="FLT_INFO.dptCorporation"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item
          label="法人代表身份证号: "
          label-width="155px"
          prop="dptCorporationId"
          :rules="rules.dptCorporationId"
        >
          <el-input
            :disabled="disabled"
            maxlength="37"
            style="width: 100%"
            v-model="FLT_INFO.dptCorporationId"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item
          label="纳税人识别号: "
          label-width="155px"
          prop="dptBusinessLicenseNo"
          :rules="rules.dptBusinessLicenseNo"
        >
          <el-input
            :disabled="disabled"
            maxlength="37"
            style="width: 100%"
            v-model="FLT_INFO.dptBusinessLicenseNo"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item
          :rules="rules.dptPredptId"
          @click.native="getCompanys"
          label="上级机构："
          label-width="155px"
          prop="dptPredptId"
        >
          <el-popover
            :disabled="disabled"
            placement="bottom-start"
            trigger="click"
            v-model="showpop"
          >
            <el-tree
              :disabled="disabled"
              :data="dptFltIdList"
              :props="defaultProps"
              node-key="dptPredptId"
              default-expand-all
              ref="tree"
              @node-click="chose"
            ></el-tree>
            <el-input
              :disabled="disabled"
              placeholder="请选择上级机构"
              slot="reference"
              v-model="FLT_INFO.dptPredptName"
            ></el-input>
          </el-popover>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="备注：" label-width="155px" prop="remark" :rules="rules.remark">
          <el-input
            :disabled="disabled"
            maxlength="150"
            resize="none"
            rows="10"
            style="width: 100%"
            type="textarea"
            v-model="FLT_INFO.remark"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row class="el-dialog__footer">
      <el-col :span="24">
        <el-form-item>
          <el-button @click="close">关闭</el-button>
          <el-button v-if="!disabled" @click="save" type="primary">保存</el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
import { getCompanysList } from '@/request/api'
import store from '@/store'

export default {
  name: 'dialog',
  props: {
    flt_info: {
      type: Object,
      required: true
    },
    disabled: {
      type: Boolean,
      default: false
    },
    showDialog: {
      type: Boolean,
      required: false
    },
    dialogmode: {
      type: String
    }
  },
  data() {
    return {
      FLT_INFO: this.flt_info,
      dptFltIdList: [],
      showpop: false,
      defaultProps: {
        children: 'children',
        label: 'dptName'
      }
    }
  },
  watch: {
    flt_info() {
      this.FLT_INFO = this.flt_info
    }
  },
  computed: {
    // 是否新增操作
    isAdd() {
      return this.dialogmode === this.DIALOG_MODE.ADD
    },
    rules() {
      return {
        radio: 'JT',
        dptSeq: [
          {
            required: true,
            message: '请输入集团/糖厂/货主序号',
            trigger: 'blur'
          }
          // {
          //   pattern: /^[0-9]+$/,
          //   message: '请输入数字',
          //   trigger: ['blur','change']
          // }
        ],
        dptCode: [
          {
            required: true,
            message: '请输入集团/糖厂/货主代码',
            trigger: 'blur'
          }
          // {
          //   pattern: /^[a-zA-Z0-9 ]+$/,
          //   message: '请输入英文和数字',
          //   trigger: ['blur','change']
          // }
        ],
        dptName: [
          {
            required: true,
            message: '请输入集团/糖厂/货主名称',
            trigger: 'blur'
          }
          // {
          //   pattern: /^[\u4e00-\u9fa5]+$/,
          //   message: '只能输入中文',
          //   trigger: 'change'
          // }
        ],
        dptAbbreviation: [
          {
            required: true,
            message: '请输入集团/糖厂/货主简称',
            trigger: 'blur'
          }
          // {
          //   pattern: /^[\u4e00-\u9fa5]+$/,
          //   message: '只能输入中文',
          //   trigger: 'change'
          // }
        ],
        dptAddress: [{ required: true, message: '请输入地址', trigger: 'blur' }],
        dptContact: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        dptPhone: [
          { required: true, message: '请输入联系人电话', trigger: 'blur' },
          {
            pattern: /^\d{11}$/,
            message: '请输入正确的联系人电话',
            trigger: 'blur'
          }
        ],
        dptEmail: [
          {
            required: true,
            message: '请输入联系人邮箱',
            trigger: 'blur'
          },
          {
            pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: '请输入正确的邮箱',
            trigger: ['blur', 'change']
          }
        ],
        dptCorporation: [{ required: true, message: '请输入法人代表', trigger: 'blur' }],
        dptCorporationId: [
          // { required: true, message: '请输入法人代表身份证号', trigger: 'blur' },
          // {
          //   validator: (rule, value, callback) => {
          //     if (/^\d{17}(\d|X)$/.test(value)) {
          //       callback()
          //     } else {
          //       callback(new Error('请输入正确的法人身份证号码'))
          //     }
          //   },
          //   trigger: 'blur'
          // }
          {
            pattern: /^\d{17}(\d|X)$/,
            message: '请输入正确的法人代表身份证号码',
            trigger: 'blur'
          }
        ],
        dptBusinessLicenseNo: [
          {
            required: true,
            message: '请输入纳税人识别号',
            trigger: 'blur'
          }
          // {
          //   pattern: /^\d{0,20}$/,
          //   message: '请输入20位有效纳税人识别号',
          //   trigger: 'blur'
          // }
        ],
        dptPredptId: [
          {
            required: true,
            message: '请选择上级机构',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  mounted() {
    this.$refs.FLT_INFO.clearValidate()
  },
  methods: {
    //点击机构类型获取默认上级机构
    getDptPre(val) {
      if (val.target._value == 'JT' || val.target._value == 'HZ') {
        this.FLT_INFO.dptPredptId = '-1'
        this.FLT_INFO.dptPredptName = '泛糖科技'
      } else {
        this.FLT_INFO.dptPredptId = ''
        this.FLT_INFO.dptPredptName = ''
      }
    },
    chose(data) {
      // 上级货种
      this.FLT_INFO.dptPredptName = data.dptName
      this.FLT_INFO.dptPredptId = data.dptId
      this.showpop = false
    },
    close() {
      // 关闭时清楚验证信息
      this.$emit('close')
    },
    save() {
      this.$refs['fltForm'].validate(valid => {
        if (valid) {
          this.$emit('save', this.FLT_INFO)
        }
      })
    },
    getCompanys() {
      if (this.FLT_INFO.dptType == 'HZ' || this.FLT_INFO.dptType == 'JT') {
        this.dptFltIdList = [
          {
            dptId: store.state.user.FLT_UUID_DX_H,
            dptName: store.state.user.FLT_NAME_DX_H,
            children: []
          }
        ]
      } else {
        getCompanysList({ dptType: 'JT' }).then(res => {
          this.dptFltIdList = res.data.content
        })
        console.log(this.FLT_INFO, 'aaaaaaaaaaaaaaaaaaaaaaaaa')
      }
    }
  },
  created() {
    console.log(this.flt_info)
    this.getCompanys()
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-popper {
  width: 300px;
  left: 460px;
  margin-left: -265px;
}

.tree {
  padding-left: 10px;
  font-size: 1.4rem;
}
</style>
